home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 25 / CU Amiga Magazine's Super CD-ROM 25 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-08].iso / CUCD / WWW / http / www.cu-amiga.co.uk / features / c-tutorial / Part-13.lzx / Part-13 / setf2.c < prev   
C/C++ Source or Header  |  2003-02-01  |  7KB  |  277 lines

  1. #include<exec/libraries.h>
  2. #include<exec/memory.h>
  3. #include<graphics/text.h>
  4. #include<intuition/intuition.h>
  5. #include<intuition/screens.h>
  6. #include<libraries/gadtools.h>
  7. #include<utility/tagitem.h>
  8.  
  9. #include<string.h>
  10. #include<stdio.h>
  11.  
  12. /* The following few includes assume the PatchLibV6.lha archive */
  13. /* has been unarchived to the same directory as this project */
  14. #include "patchlib/include/patch.h"
  15. #include "patchlib/include/patchtags.h"
  16. #include "patchlib/include/clib/patch_protos.h"
  17.  
  18. #include<clib/alib_protos.h>
  19. #include<clib/exec_protos.h>
  20. #include<clib/gadtools_protos.h>
  21. #include<clib/intuition_protos.h>
  22.  
  23. /* The library base global variables */
  24. struct Library* IntuitionBase;
  25. struct Library* GadToolsBase;
  26. struct Library* PatchBase;
  27.  
  28. /* Need to give prototypes for our functions */
  29. void handleIDCMP(struct Window*);
  30. void setupWindow();
  31. void createWindow(struct Gadget*);
  32.  
  33. #define MYFONTSIZE        (8)
  34.  
  35. /* Some constants for the size of the window */
  36. #define MYWIN_WIDTH        (410)
  37. #define MYWIN_HEIGHT    (210)
  38.  
  39. /* Some constants for the position and size of our gadget */
  40. #define MYGAD_LEFT        (10)
  41. #define MYGAD_TOP            (10+MYFONTSIZE)
  42. #define MYGAD_WIDTH        (MYWIN_WIDTH-MYGAD_LEFT*2)
  43. #define MYGAD_HEIGHT    (MYWIN_HEIGHT-MYGAD_TOP*2+MYFONTSIZE)
  44. #define MYGAD_TEXT        ("Result    Task                 Library          ")
  45. #define MYGAD_ID            (0)
  46.  
  47. /* Initialised structure declaration: describes standard Topaz font */
  48. static struct TextAttr topazFont = { "topaz.font", MYFONTSIZE, 0, 0, };
  49.  
  50. static struct Window* win;
  51. static struct Gadget* listgad;
  52. static struct List mylist;
  53. static struct SignalSemaphore ready;
  54.  
  55. /* The library offset of the OpenLibrary() call */
  56. /* (See <pragmas/exec_pragmas.h> for LVOs) */
  57. #define LVO_OPENLIBRARY (-0x228)
  58.  
  59. void addNode(char* name, struct Library* lib)
  60. {
  61.     struct Node* node = AllocVec(sizeof(struct Node), MEMF_PUBLIC | MEMF_CLEAR);
  62.     if(node && name)
  63.     {
  64.         struct Task* task = FindTask(NULL);
  65.         char* taskname = task->tc_Node.ln_Name;
  66.         int size = strlen(taskname);
  67.         if(size < 20)
  68.             size = 20;
  69.         size += 10+1+strlen(name)+1;
  70.         if(node->ln_Name = AllocVec(size, MEMF_PUBLIC))
  71.             sprintf(node->ln_Name, "$%08lx %-20s %s", lib, taskname, name);
  72.         GT_SetGadgetAttrs(listgad, win, NULL, GTLV_Labels, ~0, TAG_DONE);
  73.         AddHead(&mylist, node);
  74.         GT_SetGadgetAttrs(listgad, win, NULL, GTLV_Labels, &mylist, TAG_DONE);
  75.     }
  76. }
  77.  
  78. void freeNode(struct Node* node)
  79. {
  80.     FreeVec(node->ln_Name);
  81.     FreeVec(node);
  82. }
  83.  
  84. void freeList()
  85. {
  86.     struct Node* work;
  87.     struct Node* next = mylist.lh_Head;
  88.     while(next->ln_Succ)
  89.     {
  90.         work = next;
  91.         next = next->ln_Succ;
  92.         freeNode(work);
  93.     }
  94. }
  95.  
  96. #define USEXRES
  97.  
  98. #ifdef USEXRES
  99. struct PatchXResult* __saveds newf(register __a1 STRPTR name,
  100.                                                                      register __d0 struct Library* result)
  101. {
  102.     struct PatchXResult *xresult;
  103.     if (xresult = (struct PatchXResult*)PatchAlloc(PS_TYPE_XRESULT))
  104.     {
  105.         if(AttemptSemaphore(&ready))
  106.         {
  107.             addNode(name, result);
  108.             ReleaseSemaphore(&ready);
  109.         }
  110.         xresult->pxr_RegPattern = PATREG_D0;
  111.         xresult->pxr_RegD0 = (ULONG)result;
  112.     }
  113.     return xresult;
  114. }
  115. #else
  116. struct Library* __saveds newf(register __a1 STRPTR name,
  117.                                                             register __d0 struct Library* result)
  118. {
  119.     if(AttemptSemaphore(&ready))
  120.     {
  121.         addNode(name, result);
  122.         ReleaseSemaphore(&ready);
  123.     }
  124.     return result;
  125. }
  126. #endif
  127.  
  128. /* The start of the program */
  129. void main()
  130. {
  131.     /* Open libraries... */
  132.     if(IntuitionBase = OpenLibrary("intuition.library",37))
  133.     {
  134.         if(GadToolsBase = OpenLibrary("gadtools.library",37))
  135.         {
  136.             if(PatchBase = OpenLibrary("patch.library",5))
  137.             {
  138.                 struct Patch* patch;
  139.                 /* Set up our semaphore and lock it */
  140.                 InitSemaphore(&ready);
  141.                 ObtainSemaphore(&ready);
  142.                 patch = InstallPatchTags((APTR)&newf, LVO_OPENLIBRARY,
  143.                                                                  PATT_LibraryName,    "exec.library",
  144.                                                                  PATT_PatchName,        "MyOLPatch",
  145.                                                                  PATT_Priority,            -5,
  146.                                                                  PATT_StackSize,        1500,
  147. #ifdef USEXRES
  148.                                                                  PATT_UseXResult,        TRUE,
  149. #endif
  150.                                                                  TAG_DONE);
  151.                 /* Now do the real work */
  152.                 setupWindow();
  153.                 RemovePatchTags(patch, PATT_TimeOut, 800, TAG_DONE);
  154.                 CloseLibrary(PatchBase);
  155.             }
  156.             else
  157.                 printf("Error: could not open patch.library V5+\n");
  158.             CloseLibrary(GadToolsBase);
  159.         }
  160.         else
  161.             printf("Error: could not open gadtools.library\n");
  162.         CloseLibrary(IntuitionBase);
  163.     }
  164.     else
  165.         printf("Error: could not open intuition.library\n");
  166. }
  167.  
  168. /* Setup the window -- do the GadTools stuff */
  169. void setupWindow()
  170. {
  171.     struct Screen* scr;
  172.     /* We'll copy the visual information for the default public screen */
  173.   /* (usually, this is the Workbench screen) */
  174.     if(scr = LockPubScreen(NULL))
  175.     {
  176.         APTR vinfo;
  177.         /* Get the visual info so GadTools can render the gadgets nicely */
  178.         if(vinfo = GetVisualInfo(scr, TAG_DONE))
  179.         {
  180.             struct Gadget* glist = NULL;
  181.             if(listgad = CreateContext(&glist))
  182.             {
  183.                 struct NewGadget newgad;
  184.                 /* The offsets of our window borders */
  185.                 int offleft = scr->WBorLeft;
  186.                 int offtop = scr->WBorTop + (scr->Font->ta_YSize + 1);
  187.                 /* Setup our first gadget */
  188.                 newgad.ng_TextAttr         = &topazFont;
  189.                 newgad.ng_VisualInfo     = vinfo;
  190.                 newgad.ng_LeftEdge         = MYGAD_LEFT + offleft;
  191.                 newgad.ng_TopEdge         = MYGAD_TOP + offtop;
  192.                 newgad.ng_Width             = MYGAD_WIDTH;
  193.                 newgad.ng_Height             = MYGAD_HEIGHT;
  194.                 newgad.ng_GadgetText    = MYGAD_TEXT;
  195.                 newgad.ng_GadgetID        = MYGAD_ID;
  196.                 newgad.ng_Flags                = 0;
  197.                 NewList(&mylist);
  198.                 /* Now create it and add it to our list */
  199.                 if(listgad = CreateGadget(LISTVIEW_KIND, listgad, &newgad,
  200.                                                                     GTLV_Labels, &mylist,
  201.                                                                     TAG_DONE))
  202.                     createWindow(glist);
  203.                 else
  204.                     printf("Error: could not create gadget(s)\n");
  205.                 /* Free the gadget */
  206.                 FreeGadgets(glist);
  207.                 freeList();
  208.             }
  209.             else
  210.                 printf("Error: could not create GadTools context\n");
  211.             FreeVisualInfo(vinfo);
  212.         }
  213.         else
  214.             printf("Error: could not get visual info\n");
  215.         UnlockPubScreen(NULL, scr);
  216.     }
  217.     else
  218.         printf("Error: could not lock public screen\n");
  219. }
  220.  
  221. /* Actually open the window, in the normal way */
  222. void createWindow(struct Gadget* glist)
  223. {
  224.     /* Open our window */
  225.     if(win = OpenWindowTags(NULL,
  226.                                                     WA_InnerWidth,    MYWIN_WIDTH,
  227.                                                     WA_InnerHeight,    MYWIN_HEIGHT,
  228.                                                     WA_Flags,        WFLG_CLOSEGADGET | WFLG_DRAGBAR,
  229.                                                     WA_IDCMP,        IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW,
  230.                                                     WA_Gadgets,    glist,
  231.                                                     TAG_DONE,        0))
  232.     {
  233.         /* Let GadTools refresh its bits of the window */
  234.         GT_RefreshWindow(win, NULL);
  235.         /* Now handle messages */
  236.         handleIDCMP(win);
  237.         CloseWindow(win);
  238.     }
  239.     else
  240.         printf("Error: could not open window\n");
  241. }
  242.  
  243. /* Our message handling code */
  244. void handleIDCMP(struct Window* win)
  245. {
  246.     int going = TRUE;
  247.     /* Let the patch work */
  248.     ReleaseSemaphore(&ready);
  249.     while(going)
  250.     {
  251.         struct IntuiMessage* intuimsg;
  252.         /* Wait for messages to arrive */
  253.         WaitPort(win->UserPort);
  254.         /* Messages have arrived: loop through all of them */
  255.         while(intuimsg = GT_GetIMsg(win->UserPort))
  256.         {
  257.             /* Act on this message... */
  258.             switch(intuimsg->Class)
  259.             {
  260.             case IDCMP_CLOSEWINDOW:
  261.                 going = FALSE;
  262.                 break;
  263.             case IDCMP_REFRESHWINDOW:
  264.                 /* You *MUST* remember to ask for and handle these refresh messages */
  265.                 GT_BeginRefresh(win);
  266.                 GT_EndRefresh(win, TRUE);
  267.                 break;
  268.             }
  269.             /* Reply when finished with message */
  270.             GT_ReplyIMsg(intuimsg);
  271.         }
  272.     }
  273.     /* Stop the patched function working */
  274.     ObtainSemaphore(&ready);
  275. }
  276.  
  277.